<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 2018/1/21
 * Time: 15:16
 */
namespace common\models;

use common\models\ar\Images;
use yii\db\Query;

class UserInfoDetailDao
{
    /**
     * 获取某个用户的实名资料
     *
     * @param $uid
     * @param $menuID
     * @param int $forceGetNewRealNameFlag 是否强制获取最新提交的实名资料信息。实名栏目比较特殊，用户提交需要后台审核通过后才能更新user_info_detail表
     *
     * @return array
     */
    public static function info($uid, $menuID, $forceGetNewRealNameFlag = 1)
    {
        //所有实名信息的字段及信息
        $fields = [];
        $query = new Query();
        $tmp = $query->select("a.*,b.`field_value`")
            ->from("`user_info_field` a")
            ->leftJoin("`user_info_detail` b", "b.`field_id` = a.`id` AND b.`userID` = $uid ")
            ->where(["a.`menu_id`" => $menuID, "a.`show_flag`" => 1])
            ->orderBy(["a.`sort`" => "asc"])
            ->all();
//        var_dump($tmp);
//        exit();
        foreach ($tmp as $v) {
            $v['is_update'] = 0;
            $fields[$v['id']] = $v;
        }

        //实名栏目比较特殊，用户提交的需要后台审核通过后才能更新user_info_detail表，但是客户端又要看到自己最新提交的信息
        if ($menuID == 6 && $forceGetNewRealNameFlag == 1) {
            $query = new Query();
            $tmp = $query->select("a.`field_value`,b.*")
                ->from("`real_name_tmp` a")
                ->leftJoin("`user_info_field` b", "a.`field_id` = b.`id`")
                ->where(['userID' => $uid])
                ->all();
            foreach ($tmp as $v) {
                if (isset($fields[$v['id']])) {
                    $v['is_update'] = 1;
                    $fields[$v['id']] = $v;
                }
            }
        }

        //图片类型字段，需要到images表中去获取具体的值
        foreach ($fields as &$v) {
            if ($v['type'] == 5) {
                if ((int)$v['field_value']) {
                    $tmp = Images::findOne((int)$v['field_value']);
                    if ($tmp) {
                        $v['field_value'] = $tmp['value'];
                    }
                }
            }
        }

        return $fields;
    }
}